package org.game.manager;

import com.google.common.collect.Lists;
import org.cfg.cache.DiscreteDataCfgCache;
import org.constant.DiscreteDataID;
import org.constant.DiscreteDataKey;
import org.game.db.bean.ElementCollection;
import org.game.db.bean.Player;
import org.game.module.elementcollectbo.ElementCollectBO;
import org.game.msgBuilder.ElementCollectMsgBuilder;

import java.util.HashMap;
import java.util.List;

/**
 * 图鉴管理器
 * Created by fxf on 2018-3-1.
 */
public class ElementCollectManager {

    private ElementCollectManager(){}

    private static List<Integer> collectItemType = null;

    /**
     * 记录收集到的元素
     * @param player 玩家
     * @param type 元素类型
     * @param cid 元素cid
     */
    public static void recordElement(Player player,int type, int cid){
        ElementCollection ec = player.getElementCollectionsFkPlayerId();
        if (ec == null){
            ec = new ElementCollection(0L,new HashMap(),player.getId());
            ec.insert();
        }
        List<Integer> list = (List<Integer>) ec.getElementMap().computeIfAbsent(type, key -> Lists.newArrayList());
        if (list.contains(cid)){
            return;
        }
        list.add(cid);
        ec.update();
        player.getPlayerProxy().sendMsg(ElementCollectBO.addNewElement, ElementCollectMsgBuilder.buildAddNewElementMsg(type,cid));
    }

    /**
     * 检查物品类型是否需要记录
     * @param type
     * @return
     */
    public static boolean checkItemType(int type){
        if (collectItemType == null){
            collectItemType = Lists.newArrayList();
            List<Integer> types = (List<Integer>) DiscreteDataCfgCache.me().getData(DiscreteDataID.COLLECT_ITEM_TYPES, DiscreteDataKey.COLLECT_ITEM_TYPES);
            collectItemType.addAll(types);
        }
        return collectItemType.contains(type);
    }

}
